package com.nativetalk.controller.app.three.course;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.book.TdNtBookCourse;
import com.nativetalk.bean.comment.TdNtCommentCouse;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.course.TwoTsNtCourseInfoDetail;
import com.nativetalk.bean.course.catalog.TdNtCourseCatalog;
import com.nativetalk.bean.level.TdNtCourseLevelDetail;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.course.TdNtMembCourseDetail;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.member.course.search.MembCourseSearchService;
import com.nativetalk.service.membsetmeal.search.MembSetmealSearchService;
import com.nativetalk.service.three.book.search.ThreeBookSearchService;
import com.nativetalk.service.three.course.CourseService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeCourseController {
    private static Logger logger = Logger.getLogger(ThreeCourseController.class);
    @Resource
    private MembCourseSearchService membCourseSearchService;
    @Resource
    private MembSetmealSearchService membSetmealSearchService;
    @Resource
    private ThreeBookSearchService threeBookSearchService;
    @Resource
    private CourseService courseService;

    /**
     * @return RetInfo
     * @Purpose 教材信息
     * @version 3.3
     * @time 2016-09-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/9/course/index", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo index(@RequestHeader(value = "AppType", required = false, defaultValue = "A") String app_type) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + "app_type:" + app_type);
            if(StringUtils.isBlank(app_type)){
                app_type=Constants.AppType.A;
            }

            RetInfo retInfo = courseService.index(app_type);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找课程列表，显示套餐的最低价格
     * @version 3.3
     * @time 2016-09-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/9/course/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list(@RequestBody Map<String, String> map) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = courseService.list(map.get("cate_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找体验课列表
     * @version 3.3
     * @time 2016-09-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/9/trial/course/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo courseList(@RequestBody Map<String, String> map) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = courseService.getlist(map.get("cate_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 课程详情
     * @version 2.1
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/course/index", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo index(@RequestBody Map<String, String> mapParam,
                         @RequestHeader(value = "token", required = false) String token) {
        RetInfo retInfo = new RetInfo();
        Map<String, Object> map = new HashMap<>();
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(mapParam));

            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            map.put("free", tsNtWebSiteConfIndexMap.get("9").getData_value());
            map.put("free_start", tsNtWebSiteConfIndexMap.get("10").getData_value());
            map.put("free_end", tsNtWebSiteConfIndexMap.get("11").getData_value());
            map.put("share", tsNtWebSiteConfIndexMap.get("12").getData_value());
            map.put("share_name", tsNtWebSiteConfIndexMap.get("13").getData_value());
            map.put("share_url", tsNtWebSiteConfIndexMap.get("14").getData_value());
            map.put("share_picutre", tsNtWebSiteConfIndexMap.get("15").getData_value());
            map.put("share_content", tsNtWebSiteConfIndexMap.get("16").getData_value());

            // 课程详情
            TsNtCourseInfoDetail course = courseService.findByCouId(Integer.parseInt(mapParam.get("cou_id")));
            map.put("course", course);

            // 目录
            Map<String, Map<String, List<TdNtCourseCatalog>>> tdNtCourseCatalogLevelMap = (Map<String, Map<String, List<TdNtCourseCatalog>>>) MemcachedUtils.get(MemcachedKey.tdNtCourseCatalogLevelMap);
            map.put("level", tdNtCourseCatalogLevelMap.get(mapParam.get("cou_id")));
            Map<String, List<TdNtCourseLevelDetail>> tdNtCourseLevelDetailsMap = (Map<String, List<TdNtCourseLevelDetail>>) MemcachedUtils.get(MemcachedKey.tdNtCourseLevelDetailsMap);
            map.put("levelDetail", tdNtCourseLevelDetailsMap.get(mapParam.get("cou_id")));

            // 评论
            List<TdNtCommentCouse> tdNtCommentCouses = courseService.findCommentByCouId(mapParam.get("cou_id"));
            map.put("comment", tdNtCommentCouses);
            map.put("totalRecord", tdNtCommentCouses.size());// 评论数
            map.put("totalPage", 0);//评论总页数

            List<TdNtCoursePictureList> coursePictureLists =courseService.findCoursePicture(mapParam.get("cou_id"));
            if (coursePictureLists == null) {
                coursePictureLists = new ArrayList<>();
            }

            // 如果学生已登录系统，则查询该学生是否购买过体验课
            Object o = null;
            if (StringUtils.isNotBlank(token)) {
                o = MemcachedUtils.get(token);
            }
            if (o != null && TdNtMember.class.isInstance(o)) {
                TdNtMember tdNtMember = (TdNtMember) o;
                // 分享课程，添加自己的邀请码
                map.put("share_url", tsNtWebSiteConfIndexMap.get("14").getData_value() + "?invite_code=" + tdNtMember.getMemb_invite_code());
            }
            map.put("coursePictureSize", coursePictureLists.size());

            // 偷偷瞄一眼 课程的教材，最多只能瞄5张ppt图片
            if (coursePictureLists.size() >= 5) {
                map.put("coursePicture", coursePictureLists.subList(0, 5));
            } else {
                map.put("coursePicture", coursePictureLists);
            }
            map.put("is_buy", 1);
            map.put("coursePictureSize", 1);
            map.put("isTrial", 0);
            map.put("studySize", 0);
            map.put("teacher", Constants.teacher);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取教程数据成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }

}


